import {Injectable} from '@angular/core';
import {Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {HttpClientHelper} from '../app.httpclient';
import 'rxjs/Rx';

@Injectable()
export class UserService {
  private loginUrl: string = 'users/login';
  private meUrl: string = 'users/me';
  private registerUrl: string = 'users/register';
  private logoutUrl: string = 'users/logout';
  private userForgotUrl: string = 'users/forgotpassword';
  private userResetPassUrl: string = 'users/passwordreset?user_id=';
  private facebookLoginUrl: string = 'auth/facebook/token?access_token=';
  private getIdeaByAuthor = 'users/getAuthor';
  private httpClient: HttpClientHelper;
  private viewProfile = 'users/viewProfile';
  private updateProfile = 'users/updateProfile';
  private updateProfileImageSaved = 'users/uploadCropped';
  private allAdminUsers = 'users/getAdminUsers';
  private updateStatusOfUser = 'users/updateStatus';
  private abuseUser = 'abuses/getAllAbuses';
  private addNewUser = 'users/addUser';
  private editNewUser = 'users/editUser';
  private getAllDescription = 'versions/getAllVersions';
  public getRecentUsersDash = 'users/getRecentUsers';
  public setVersionCountsAdd = 'versions/setVersionCountAdd';
  public setVersionCountsSub = 'versions/setVersionCountSub';
  public getVersioningData = 'versions/getVersioning';
  public userLog = 'userLogs/createLog';
  public sendUserLog = 'userLogs/getAllLogsByDate';
  public sendActivityLog = 'activity/getAllActivities';
  public versionLock = 'versions/changeLockStatus';
  public getCSVFileforAll = 'userLogs/getAllLogsCSV';
  public getTimelineValue = 'sequences/getUserSequence'
  public setTrackRecordUrl = 'sequences/createSequence';
  public timelineCSV = 'sequences/getSequenceCSV';

  constructor(httpClient: HttpClientHelper) {
    this.httpClient = httpClient;
  }

  // For signup service
  userSignup(data): Observable<any> {
    return this.httpClient.postPreLogin(this.registerUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  userLogin(data): Observable<any> {
    return this.httpClient.postPreLogin(this.loginUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  // For signup service
  userLogout(data): Observable<any> {
    return this.httpClient.post(this.logoutUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  // Send Forgot Password Link
  userForgot(data): Observable<any> {
    return this.httpClient.postPreLogin(this.userForgotUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  // Send Reset password with new password
  userReset(data): Observable<any> {
    let headers = new Headers();
    console.log(data);
    this.userResetPassUrl = this.userResetPassUrl + data.user_id;
    headers.append('Content-Type', 'application/json');
    headers.append('token', data.token);
    // delete data.token;
    // delete data.user_id;
    return this.httpClient.postByHeaderPreLogin(this.userResetPassUrl, data, headers)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  facebookLogin(data): Observable<any> {
    this.facebookLoginUrl = this.facebookLoginUrl + data.token;
    return this.httpClient.post(this.facebookLoginUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  private extractResponse(res: JSON) {
    
    return res;
  }

  private handleError(error: any) {
    error.error_message = error.error.message;
    return Observable.throw(error);
  }

  getideabyAuthor(data): Observable<any> {
    return this.httpClient.post(this.getIdeaByAuthor, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  userProfile(data): Observable<any> {
    return this.httpClient.post(this.viewProfile, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  userProfileUpdateChange(data): Observable<any> {
    return this.httpClient.post(this.updateProfile, data)
      .map(this.extractResponse)
      .catch(this.handleError);

  }

  userProfileImageSaved(data): Observable<any> {
    return this.httpClient.post(this.updateProfileImageSaved, data)
      .map(this.extractResponse)
      .catch(this.handleError);

  }

  getAllUser(data): Observable<any> {
    return this.httpClient.post(this.allAdminUsers, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  updateStatus(data): Observable<any> {
    return this.httpClient.post(this.updateStatusOfUser, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getAbuseData(data): Observable<any> {
    return this.httpClient.post(this.abuseUser, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  addUser(data): Observable<any> {
    return this.httpClient.post(this.addNewUser, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  editUser(data): Observable<any> {
    return this.httpClient.post(this.editNewUser, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getAllVersion(data): Observable<any> {
    return this.httpClient.post(this.getAllDescription, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  setVersionCountAdd(data): Observable<any> {
    return this.httpClient.post(this.setVersionCountsAdd, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  setVersionCountSub(data): Observable<any> {
    return this.httpClient.post(this.setVersionCountsSub, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getRecentUsers(data): Observable<any> {
    return this.httpClient.post(this.getRecentUsersDash, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getuserInfo(): Observable<any> {
    return this.httpClient.post(this.meUrl, {})
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getVersionings(data): Observable<any> {
    return this.httpClient.post(this.getVersioningData, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  setUserLogs(data): Observable<any> {
    return this.httpClient.post(this.userLog, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getuserLogData(data): Observable<any> {
    return this.httpClient.post(this.sendUserLog, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getuserActivityData(data): Observable<any> {
    return this.httpClient.post(this.sendActivityLog, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  versionLockUnlock(data): Observable<any> {
    return this.httpClient.post(this.versionLock, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }


  getLogAllCSV(data): Observable<any> {
    return this.httpClient.post(this.getCSVFileforAll, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getTimelineData(data): Observable<any> {
    return this.httpClient.post(this.getTimelineValue, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  setUrlSeq(data): Observable<any> {
    return this.httpClient.post(this.setTrackRecordUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getTimelineCSV(data): Observable<any> {
    return this.httpClient.post(this.timelineCSV, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

}
